package com.alex.users;

import controller.DBController;
import net.sf.json.JSONObject;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

@Controller
public class MainUserController {

    @RequestMapping(value = "/user/register",method = RequestMethod.POST)
    public ModelAndView registerinfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ModelAndView modelAndView = null;

        String uid = "1000";
        String registerPhone = request.getParameter("phonenum");
        String secrect = request.getParameter("secrect");
        String reSecrect = request.getParameter("rescrect");
        if (secrect != reSecrect){
            System.out.println("两次输入密码不相投");
            return modelAndView;
        }

        // 此处注册用户信息，链接数据库，并获取用户注册以后的信息
        Map<String,String> userinfo = DBController.getInstance().getuserinfo(uid);
        String data = "中国 GET";

        OutputStream stream = response.getOutputStream();

        stream.write(data.getBytes("UTF-8"));

        return modelAndView;
    }
    @RequestMapping(value = "/user/getinfo",method = RequestMethod.GET)
    public ModelAndView getuserinfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ModelAndView modelAndView = null;
        response.setHeader("Content-type","text/html;charset=UTF-8");//向浏览器发送一个响应头，设置浏览器的解码方式为UTF-8

        String uid = request.getParameter("uid");;

        // 此处注册用户信息，链接数据库，并获取用户注册以后的信息
        Map<String,String> userinfo = DBController.getInstance().getuserinfo(uid);
        JSONObject jsonObject = JSONObject.fromObject(userinfo);

        String data = jsonObject.toString();

        OutputStream stream = response.getOutputStream();

        stream.write(data.getBytes("UTF-8"));

        return modelAndView;
    }
}
